import 'package:ceramics/extensions/ab_widget_extensions.dart';
import 'package:ceramics/model/ab_works.dart';
import 'package:ceramics/pages/main/home/ab_home_detail_page.dart';
import 'package:ceramics/pages/main/home/ab_home_works_item.dart';
import 'package:ceramics/pages/main/hot/ab_hot_works_item.dart';
import 'package:ceramics/pages/main/publish/ab_publish_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ABHotController extends GetxController {
  var works = <ABWorks>[].obs;

  @override
  onInit() {
    super.onInit();
    loadData();
  }

  Future<void> loadData() async {
    works.value = await ABWorks.hotWorks();
  }
}

class ABHotPage extends GetView<ABHotController> {
  const ABHotPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leadingWidth: 200,
        backgroundColor: Colors.white.withOpacity(0.1),
        leading: Row(
          children: [
            20.toHorizontalBox,
            Image.asset('images/ceramics_56.webp', width: 150),
          ],
        ),
        title: Row(
          children: [
            const Spacer(),
            Image.asset('images/ceramics_84.webp', scale: 3).onTap(()=>Get.to(()=>const ABPublishPage())),
          ],
        ),
      ),
      extendBodyBehindAppBar: true,
      body: Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage("images/login_bg.webp"),
            fit: BoxFit.cover,
          ),
        ),
        child: SafeArea(
          bottom: false,
          child: Obx(() {
            return ListView.separated(itemBuilder: (ctx, idx) {
              var model = controller.works[idx];
              return ABHotWorksItem(works: model, idx: idx).onTap(() {
                Get.to(() => ABHomeDetailPage(id: model.id.toString()),
                    arguments: controller.works[idx]);
              });
            }, separatorBuilder: (ctx, idx) {
              return 40.toVerticalBox;
            }, itemCount: controller.works.length,
                padding: const EdgeInsets.only(
                    right: 10, bottom: kBottomNavigationBarHeight + 80)
            );
          }),
        ),
      ),
    );
  }
}
